<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    :immediate-check="false"
    @load="onLoad"
  >
    <CommentItem
    v-for="item in list"
    :key="item.com_id + ''"
    :title="item.aut_name"
    :comment='item'
    @on-reply="$emit('on-reply', $event)" />
  </van-list>
</template>

<script>
import { getCommomlist } from '@/api/comment.js'
import CommentItem from '@/components/comment-item'
export default {
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  components: {
    CommentItem
  },
  props: {
    type: {
      type: String,
      required: true
    },
    source: {
      type: [String, Number, Object],
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },
  created () {
    this.onLoad()
  },

  methods: {
    async onLoad () {
      try {
        const { data: res } = await getCommomlist({
          type: this.type,
          source: this.source,
          offset: this.offset,
          limit: this.limit
        })
        this.list.push(...res.data.results)
        this.$emit('onload-success', res.data.total_count)
        this.loading = false
        if (res.data.results.length) {
          // 有数据
          this.offset = res.data.last_id
        } else {
          this.finished = true
        }
      } catch (err) {
        this.$toast('获取失败!')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
